package com.star.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.star.context.BaseContext;
import com.star.dto.CommentsDTO;
import com.star.dto.CommentsPageQueryDTO;
import com.star.entity.Comments;
import com.star.mapper.CommentsMapper;
import com.star.result.PageResult;
import com.star.service.CommentsService;
import com.star.vo.CommentsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class CommentsServiceImpl implements CommentsService {

    @Autowired
    private CommentsMapper commentsMapper;

    /**
     * 评论分页查询
     * @param commentsPageQueryDTO
     * @return
     */
    public PageResult pageQuery(CommentsPageQueryDTO commentsPageQueryDTO) {
        PageHelper.startPage(commentsPageQueryDTO.getPageQueryDTO().getPage(), commentsPageQueryDTO.getPageQueryDTO().getPageSize());
        Page<CommentsVO> page = commentsMapper.pageQuery(commentsPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增评论
     * @param commentsDTO
     */
    public void addComment(CommentsDTO commentsDTO) {
        Comments comments = new Comments();
        BeanUtils.copyProperties(commentsDTO,comments);
        comments.setUserId(BaseContext.getCurrentId());
        comments.setCommentTime(LocalDateTime.now());
        commentsMapper.insert(comments);
    }
}
